#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    int minDistance(string word1, string word2)
    {
        int m = word1.size();
        int n = word2.size();
        vector<vector<int>> cost(m + 1, vector<int>(n + 1));
        for (int i = 0; i <= m; i++)
        {
            cost[i][0] = i;
        }
        for (int j = 0; j <= n; j++)
        {
            cost[0][j] = j;
        }
        //以上规定由word1的前i个化到0和word2前j个化到0的情况
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (word1[i - 1] == word2[j - 1])
                    cost[i][j] = cost[i - 1][j - 1]; //即不用做任何操作的最小值
                else
                    cost[i][j] = min({cost[i - 1][j - 1], cost[i][j - 1], cost[i - 1][j]}) + 1; //上一位的最小值+1
            }
        }
        return cost[m][n];
    }
};
